package com.mxfcode.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxfcode.back.dto.PlateDTO;
import com.mxfcode.back.mapper.BackPlateMapper;
import com.mxfcode.back.service.BackPlateService;
import com.mxfcode.bbs_common.date.DateUtils;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.bbs_common.util.community.CommunityUtil;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.BBSPlate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @类名 PlateServiceImpl
 * @描述 板块管理业务
 * @作者 mxf
 * @创建日期 2021-03-05 14:02
 **/
@Service
public class BackPlateServiceImpl implements BackPlateService {
    private static final Logger logger = LoggerFactory.getLogger(BackPlateServiceImpl.class);

    @Resource
    private BackPlateMapper backPlateMapper;
    @Resource
    private HostHolder hostHolder;


    @Override
    public Map<String, Object> addPlate(BBSPlate plate) {
        Map<String,Object> data = new HashMap();
        try {
            //默认字段
            plate.setCreateDate(DateUtils.getCurrentDateToDate());
            plate.setCreateBy(hostHolder.getUser().getUserName());
            plate.setCreateName(hostHolder.getUser().getRealName());
            plate.setUpdateDate(DateUtils.getCurrentDateToDate());
            plate.setUpdateBy(hostHolder.getUser().getUserName());
            plate.setUpdateName(hostHolder.getUser().getRealName());
            plate.setDeleted(1);
            plate.setArticleCount(0L);
            plate.setId(CommunityUtil.generateUUID());
            plate.setPlateStatus(1);
            plate.setViewCount(0L);

            int result = backPlateMapper.insertPlate(plate);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("板块[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("板块[新增]，结果=新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("板块[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updatePlate(BBSPlate plate) throws Exception {
        Map<String,Object> data = new HashMap();
        try{
            plate.setUpdateName(hostHolder.getUser().getRealName());
            plate.setUpdateBy(hostHolder.getUser().getUserName());
            plate.setUpdateDate(DateUtils.getCurrentDateToDate());
            int result = backPlateMapper.updatePlate(plate);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("板块[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("板块[更新]，结果=更新成功！");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("板块[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> delPlate(String id, Integer status) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除板块
            int result = backPlateMapper.updatePlateStatus(id,status);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除板块失败");
                logger.error("删除板块失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除板块成功");
            logger.info("删除板块成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除板块异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> recoverPlate(String id, Integer status) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 恢复板块
            int result = backPlateMapper.updatePlateStatus(id,status);
            if(result == 0){
                data.put("code",0);
                data.put("msg","恢复板块失败");
                logger.error("恢复板块失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","恢复板块成功");
            logger.info("恢复板块成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("恢复板块异常！", e);
        }
        return data;
    }

    @Override
    public PageDataResult getPlateList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<PlateDTO> plateList = backPlateMapper.getPlateList();

        PageHelper.startPage(pageNum, pageSize);

        if(plateList.size() != 0){
            PageInfo<PlateDTO> pageInfo = new PageInfo<>(plateList);
            pageDataResult.setList(plateList);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }

        return pageDataResult;
    }

    @Override
    public BBSPlate getPlateById(String id) {
        BBSPlate plate = backPlateMapper.getPlateById(id);
        return plate;
    }

    @Override
    public List<BBSPlate> getAllPlateList() {
        List<BBSPlate> plateList = backPlateMapper.getAllPlateList();
        return plateList;
    }


}
